package com.rdc.qingkou.ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.rdc.qingkou.Application.MyApplication;
import com.rdc.qingkou.R;
import com.rdc.qingkou.utils.ACache;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;



public class UserInfoChooseDialogActivity extends AppCompatActivity{

    private File tempFile;

    private ACache mACache;

    private Uri imageUri;
    public static final int CUT_PICTURE = 1;
    public static final int SHOW_PICTURE = 2;

    public static final int TAKE_BIG_PICTURE = 8;   //3,
    public static final int CROP_BIG_PICTURE = 9;   //3,


    private static final String IMAGE_FILE_LOCATION = "file:///sdcard/temp.jpg";//temp file
    private Uri imageUri2 = Uri.parse(IMAGE_FILE_LOCATION);//The Uri to store the big bitmap

//    private File outputImageGallery;
//    private File outputImageCamera;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);

        setContentView(R.layout.send_addmedia);

        mACache = ACache.get(this);

        findViewById(R.id.btn_takePhoto).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                camera(v);
            }
        });
        findViewById(R.id.btn_chooseAlbum).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gallery(v);
            }
        });
    }

    public void gallery(View view) {
        mACache.put("sculptureMode",0);
        //2,
        File outputImage = new File(Environment.getExternalStorageDirectory(),"output_image.jpg");
        try {
            if (outputImage.exists()) {
                outputImage.delete();
            }
            outputImage.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        imageUri = Uri.fromFile(outputImage);
        Intent intent = new Intent(Intent.ACTION_PICK,null);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        startActivityForResult(intent, CUT_PICTURE);
    }



    public void camera(View view) {
        mACache.put("sculptureMode",1);
        //3,
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//action is capture
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri2);
        startActivityForResult(intent, TAKE_BIG_PICTURE);//or TAKE_SMALL_PICTURE
    }

    //2,
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case CUT_PICTURE:
                if (resultCode == RESULT_OK) {
                    //此处启动裁剪程序
                    Intent intent = new Intent("com.android.camera.action.CROP");
                    intent.setDataAndType(data.getData(), "image/*");  // Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference
                    intent.putExtra("scale", true);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

                    intent.putExtra("aspectX", 1);   //方形截图
                    intent.putExtra("aspectY", 1);

                    startActivityForResult(intent, SHOW_PICTURE);
                }
                break;

            case SHOW_PICTURE:
                if (resultCode == RESULT_OK) {
                    try {
                        //将output_image.jpg对象解析成Bitmap对象，然后设置到ImageView中显示出来
                        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
                        //picture.setImageBitmap(bitmap);
                        mACache.put("bitmap",bitmap);
                        finish();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
                break;

            //3,
            case TAKE_BIG_PICTURE:
//                //TODO sent to crop
                cropImageUri(imageUri2, 800, 800, CROP_BIG_PICTURE);
                break;

            case CROP_BIG_PICTURE://from crop_big_picture

                if(imageUri2 != null){

                    try {
                        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri2));
                        mACache.put("bitmap",bitmap);
                        finish();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
                break;

            default:
                break;
        }
    }

    private void cropImageUri(Uri uri, int outputX, int outputY, int requestCode){

        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", outputX);
        intent.putExtra("outputY", outputY);
        intent.putExtra("scale", true);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        intent.putExtra("return-data", false);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true); // no face detection
        startActivityForResult(intent, requestCode);
    }

}







